from django.shortcuts import render
from myapp.models import Message
from rest_framework.views import APIView
from rest_framework.response import Response
from myapp.paqu import xinxi
# Create your views here.

class MessageView(APIView):
    def get(self,request):
        message = Message.objects.all()
        message_lest = []
        for i in message:
            message_lest.append({
                'province':i.province,
                'name':i.name,
                'post':i.post,
                'punishment':i.punishment,
                'date':i.date,
            })
        return Response({
            'code':200,
            'msg':'获取成功',
            'data':message_lest
        })

    def post(self,request):
        dada = xinxi()

        for i in dada:
            print('.',i)
            if i.get('name') != '':
                Message.objects.create(
                    province=i.get('province'),
                    name=i.get('name'),
                    post=i.get('post'),
                    punishment=i.get('punishment'),
                    date=i.get('date'),
                )

        return Response({
            'code':200,
            'msg':'添加成功'
        })





